package com.manager.web.system;

import java.io.PrintWriter;

import com.manager.base.BaseAction;
import com.manager.pojo.system.QuerySql;
import com.opensymphony.xwork2.Action;

public class SysQuerysqlAction extends BaseAction {
	private QuerySql querySql;
	
	/*
	 * 添加左边栏菜单
	 */
	public String addQuerySql(){
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			
			//查询用户名是否已存在
			int total = super.baseService.queryInt("select count(1) from query_sql where des = ?", querySql.getDes());
			if(total == 1){
				request.setAttribute("msg", "添加操作失败，该描述已存在");
			}else{
				//保存基本用户信息
				QuerySql qs = new QuerySql();
				qs.setContent(querySql.getContent());
				qs.setSqlType(querySql.getSqlType());
				qs.setDes(querySql.getDes());
				super.baseService.save(qs);
				request.setAttribute("msg", "操作成功");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		super.forward("system/success.jsp");
		return Action.NONE;
	}
	
	/*
	 * 删除左边菜单
	 */
	public String deleteQuerySql(){
		PrintWriter p = null;
		try{
			p = response.getWriter();
			super.baseService.delete("delete from query_Sql where id = ?", querySql.getId());
			p.write("{\"flag\":true}");
		}catch(Exception e){
			p.write("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		return Action.NONE;
	}
	
	/*
	 * 由id查询querySql
	 */
	public String queryQuerySqlById(){
		querySql = (QuerySql)super.baseService.getObject(QuerySql.class, querySql.getId());
		super.forward("system/querysqlUpdate.jsp");
		return Action.NONE;
	}
	
	/*
	 * 更新querySql
	 */
	public String updateQuerySql(){
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			
			//查询用户名是否已存在
			int total = super.baseService.queryInt("select count(1) from query_sql where des = ? and id != ?", querySql.getDes(), querySql.getId());
			if(total == 1){
				request.setAttribute("msg", "更新操作失败，该描述已存在");
			}else{
				//更新基本用户信息
				QuerySql qs = (QuerySql)super.baseService.getObject(QuerySql.class, querySql.getId());
				qs.setContent(querySql.getContent());
				qs.setSqlType(querySql.getSqlType());
				qs.setDes(querySql.getDes());
				super.baseService.update(qs);
				request.setAttribute("msg", "操作成功");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		super.forward("system/success.jsp");
		return Action.NONE;
	}
	
	/*
	 * getter and setter
	 */
	public QuerySql getQuerySql() {
		return querySql;
	}
	
	public void setQuerySql(QuerySql querySql) {
		this.querySql = querySql;
	}
}
